package speed2010.matcher.gui;

import graph.DirectedGraphImpl;
import graph.Graph;
import graph.GraphImpl;
import graph.Vertex;
import graph.VertexImpl;
import graph.visual.GraphScrollPane;
import graph.visual.VisualGraph;
import graph.visual.layout.ForceDirectedLayout;

import java.awt.Dialog;
import java.awt.Dimension;
import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JDialog;

import o2g.Edge;
import o2g.Node;
import o2g.O2GParser;
import o2g.Ontology;

import org.semanticweb.owl.apibinding.OWLManager;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOntologyCreationException;
import org.semanticweb.owl.model.OWLOntologyManager;

import speed2010.matcher.gui.FOntologyMatcherGUI;

public class FOntologyViewer extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6291093683199236758L;
	private GraphScrollPane pnlOntologyView = null;
	private o2g.Graph ontologyGraph = null; // @jve:decl-index=0:
	private VisualGraph vGraph = null;  //  @jve:decl-index=0:

	private String ontologyFile = null; // @jve:decl-index=0:

	/**
	 * This method initializes
	 * 
	 */
	public FOntologyViewer(String file) {
		super(FOntologyMatcherGUI.INSTANCE, file, Dialog.ModalityType.APPLICATION_MODAL);
		ontologyFile = file;
		initialize();
		loadOntology();		
	}

	@SuppressWarnings("unchecked")
	private void loadOntology() {
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		ontologyFile = ontologyFile.replace("\\", "/");
		URI physicalURI = URI.create("file:///"
				+ ontologyFile.replace(" ", "%20"));
		OWLOntology owl = null;
		try {
			owl = manager.loadOntologyFromPhysicalURI(physicalURI);
		} catch (OWLOntologyCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (owl != null) {
				Ontology ont = new Ontology(owl);
				O2GParser o2g = new O2GParser(ont);
				o2g.parse();
				ontologyGraph = o2g.getGraph();

				Graph g = null;
				if (ontologyGraph.isDirected() == 1) {
					g = new DirectedGraphImpl();
				} else {
					g = new GraphImpl();
				}
				for (Node node : ontologyGraph.getNodes()) {
					Vertex v = new VertexImpl(node.getLabel());

					g.add(v);

				}
				for (Edge edge : ontologyGraph.getEdges()) {
					Vertex v1 = getVertexByLabel(g, edge.getSource().getLabel());
					Vertex v2 = getVertexByLabel(g, edge.getTarget().getLabel());
					g.addEdge(v1, v2);
				}

				java.util.List<String> backings = new java.util.ArrayList<String>();
				Map<String, Integer> back_index = new HashMap<String, Integer>();

				for (Iterator<graph.Edge> iter = g.getEdgeSet().iterator(); iter
						.hasNext();) {
					graph.Edge element = iter.next();
					element.setFollowVertexLabel(false);
					Node n1 = ontologyGraph.getNodeByLabel(element.getVertexA()
							.getLabel());
					Node n2 = ontologyGraph.getNodeByLabel(element.getVertexB()
							.getLabel());
					String label = ontologyGraph.getEdgeLabelByNodes(n1, n2);
					if (ontologyGraph.getInverseDirectional(n1, n2).length() > 0) {
						backings.add(ontologyGraph
								.getInverseDirectional(n1, n2));
						back_index.put(ontologyGraph.getInverseDirectional(n1,
								n2), label.length() * 7);
					}
					String direction = n1.getLabel() + " -> " + n2.getLabel();
					String appendix = "";
					if (backings.contains(direction)) {
						appendix = getSizeInWhiteSpaces(back_index
								.get(direction));
					}
					String result = appendix + label;
					element.setLabel(result);
				}

				vGraph = new VisualGraph(g);

				ForceDirectedLayout layout = new ForceDirectedLayout(vGraph);
				layout.setEletricalRepulsion(10);
				layout.setSpringLength(120);
				layout.setStiffness(10);
				layout.setIncrement(5);
				vGraph.setGraphLayoutManager(layout);
				layout.layout();
				pnlOntologyView.setVisualGraph(vGraph);
				
				
				
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private String getSizeInWhiteSpaces(int size) {
		String str = "";
		for (int i = 0; i < size; i++) {
			str += " ";
		}
		return str;
	}

	@SuppressWarnings("unchecked")
	private Vertex getVertexByLabel(Graph g, String label) {
		Vertex v = null;
		for (Iterator<Vertex> iterator = g.getVerticesIterator(); iterator
				.hasNext();) {
			v = iterator.next();
			if (v.getLabel().equals(label)) {
				break;
			}
		}
		return v;
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setSize(new Dimension(500, 550));
		this.setLocationRelativeTo(FOntologyMatcherGUI.INSTANCE);
		

		if (ontologyFile != null && !ontologyFile.isEmpty())
			this.setTitle(ontologyFile);
		else
			this.setTitle("Ontology Viewer");

		this.setContentPane(getPnlOntologyView());

	}

	/**
	 * This method initializes pnlOntologyView
	 * 
	 * @return javax.swing.JPanel
	 */
	private GraphScrollPane getPnlOntologyView() {
		if (pnlOntologyView == null) {
			pnlOntologyView = new GraphScrollPane();
		}
		return pnlOntologyView;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
